	import { baseUrlHttp } from "@/env/env.config";
  import { getCityInfoByLatAndLon } from "@/api/sys";
	
  /**
   * @description: 乘法
   * @param {number} num1
   * @param {number} num2
   * @return {*}
   */  
  export function multi(num1: number, num2: number) {
    let baseNum = 0;
    try {
      let arr = num1.toString().split(".");
      if (arr.length > 1) {
        baseNum += arr[1].length;
      }
    } catch (e) {
      console.error(e);
    }
    try {
      let arr = num2.toString().split(".");
      if (arr.length > 1) {
        baseNum += arr[1].length;
      }
    } catch (e) {
      console.error(e);
    }
    return (
      (Number(num1.toString().replace(".", "")) *
        Number(num2.toString().replace(".", ""))) /
      Math.pow(10, baseNum)
    );
  }

 /**
  * @description: 获取用户头像
  * @param {string} url
  * @return {*}
  */	
	export function getAvatar(url: string) {
		return url ? getImageUrl(url) : getStaticUrl('/server/tabbar5/touxiang2.png')
	}
	
 /**
  * @description: 获取房源图片(没有默认返回静态图片)
  * @param {string} url
  * @return {*} 
  */	
	export function getHouseSourceImage(url: string) {
		return url ? getImageUrl(url, '?scale=0.5') : getStaticUrl('/server/house/house_default.png')
	}
	
 /**
  * @description: 获取图片
  * @param {string} url
  * @param {string} params
  * @return {*}
  */  
	export function getImageUrl(url: string, params?: string) {
		return `${baseUrlHttp}/ihomesaas/client/client/file/view/${url}${params || ''}`;
	}
	
	
 /**
  * @description: 静态资源地址
  * @param {any} url
  * @return {*}
  */	
	export function getStaticUrl(url: any) {
	  return `https://oss.polyic.cn/aj-web-app/static${url}`
	}

  /**
   * @description: 字符串的标签组转化成数组
   * @param {string} str
   * @return {*}
   */  
  export function tagsToArr(str: string) {
    let tags: any = [];
      if (str) {
        if (str.includes(",")) {
          tags = str.split(",");
        } else {
          tags = [str];
        }
      } else {
        tags = [];
      }
      // tags = tags.splice(0, 3);
      return tags;
  }

  /**
   * @description: 百度经纬度前端转换腾讯经纬度
   * @param {number} lng 经度
   * @param {number} lat 纬度
   * @return {*}
   */  
  export function bMapTransqqMap(lng: number, lat: number) {
    let x_pi = (3.14159265358979324 * 3000.0) / 180.0;
    let x = lng - 0.0065;
    let y = lat - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta);
    let lats = z * Math.sin(theta);
    return {
      longitude: lngs,
      latitude: lats
    };
  }

  /**
   * @description: 获取当前定位
   * @param {*} isHighAccuracy
   * @param {*} isUpdateStorage
   * @return {*}
   */  
  export function getCurrentCity(isHighAccuracy = true, isUpdateStorage = true) {
    return new Promise<void>(async (resolve, reject) => {
      uni.getLocation({
        type: "gcj02",
        geocode: true,
        isHighAccuracy,
        success: async (res) => {
          console.log(res, "success");
          const item: any = await getCityInfoByLatAndLon({
            location: res.latitude + "," + res.longitude,
          });
          const cityInfo = item.result.sysArea;
          const city: any = {
            name: cityInfo.name,
            code: cityInfo.code,
            parentCode: cityInfo.parentCode
          };
					if (isUpdateStorage) {
						uni.setStorageSync("gpsCity", city);
						uni.setStorageSync("currentCity", city);
					}
					resolve(city);
        },
        fail: (err) => {
          let city: any = {
            abbr: "GZ",
            code: "440100000000",
            first: "G",
            level: 2,
            name: "广州",
            parentCode: "440000000000",
          };
          uni.setStorageSync("gpsCity", city);
          console.log(err, "定位服务未开通");
          resolve(city);
        },
      });
    });
  }
